(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.role.deleteVerifyController', editController);
	editController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'roleService' ];
	function editController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, roleService) {
		var ctrl = this, params = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.role = {};
		ctrl.roleOld = {};
		
		ctrl.init = function() {
			ctrl.roleRightGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.roleRight.tableConfigs.table, rowSelectionChanged);
			ctrl.roleRightGridOptions.data = 'vm.role.roleRights';
			ctrl.roleRightGridOptions.enableRowHeaderSelection = false;
			function rowSelectionChanged(eventRow, selectedRows) {
				ctrl.selectedDTParmList = selectedRows;
			}
			authService.deleteVerify(params).then(function(response) {
				if (response.data && response.data.model && response.data.task) {
					ctrl.role = response.data.model;
					ctrl.role.taskId = response.data.task.taskId;
					ctrl.role.roleRights = ctrl.role.roleRights || [];
					angular.forEach(ctrl.role.roleRights, function (con, key){
						if (con.dataStatus == 'V' || con.dataStatus == null) {
							con.dataStatus = 'V';
						} else {
							con.dataStatus = ctrl.role.dataStatus;
						}
					});
				}
			});
		};
		
		
		ctrl.deleteVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.role.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.deleteVerifyApprove(ctrl.role).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		ctrl.deleteVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.role.comments = comments;
						authService.deleteVerifyReject(ctrl.role).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.role.list',
					'state' : 'cda.role.list',
					'params' : {}
				}]
			});
		}

		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.workflow.wfSetup.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.role.list',
					'state' : 'cda.role.list',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
	}
})(angular);